package com.bruce.demo2.patterns.demo2.structural.demo12.flyweight.demo1;

import java.util.HashMap;
import java.util.Map;

/**
 * @PROJECT_NAME: design_patterns
 * @DESCRIPTION: 工厂类：将该类设计为单例
 * @AUTHOR: CNBruceLee
 * @DATE: 2021/8/2 6:45
 */
public class BoxFactory {
    //存放共享的对象
    private Map<String, AbstractBox> map;

    //单例的工厂类
    private static BoxFactory boxFactory = new BoxFactory();

    /**
     * 私有化构造方法，共享对象的初始化在此处初始化
     */
    private BoxFactory() {
        map = new HashMap<String, AbstractBox>();
        map.put("I", new IBox());
        map.put("L", new LBox());
        map.put("O", new OBox());
    }

    /**
     * 抽象的工厂类
     *
     * @return
     */
    public static BoxFactory getInstance() {
        return boxFactory;
    }

    /**
     * 根据名称获取图形对象
     *
     * @param name
     * @return
     */
    public AbstractBox getShape(String name) {
        return map.get(name);
    }


}
